using System;
using System.IO;
using System.IO.IsolatedStorage;
using System.Runtime.Serialization;

namespace CacheLoader
{
    /// <summary>
    /// This class hold some helpers utils.
    /// </summary>
    public static class ManifestUtil
    {
        public static void StoreManifest(CacheManifest cacheManifest)
        {
            using (IsolatedStorageFile file = IsolatedStorageFile.GetUserStoreForApplication())
            {
                var stream = file.OpenFile(cacheManifest.LocalPath, FileMode.Create, FileAccess.ReadWrite);
                DataContractSerializer serializer = new DataContractSerializer(typeof(CacheManifest));
                serializer.WriteObject(stream, cacheManifest);
                stream.Close();
            }
        }
        public static CacheManifest LoadManifest(string loaclPath)
        {
            if (string.IsNullOrEmpty(loaclPath))
                return null;
            using (IsolatedStorageFile store = IsolatedStorageFile.GetUserStoreForApplication())
            {
                if (!store.FileExists(loaclPath))
                    return null;
                var stream = store.OpenFile(loaclPath, FileMode.Open, FileAccess.Read);
                DataContractSerializer serializer = new DataContractSerializer(typeof(CacheManifest));
                var res = serializer.ReadObject(stream);
                stream.Close();
                return res as CacheManifest;
            }
        }
    }
}